package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tsurveyanswer;
import com.xy.manage.model.Tsurveyquestion;
import com.xy.manage.service.TsurveyanswerService;



@Controller
public class TsurveyanswerAction{
	private static final Logger log = LoggerFactory.getLogger(TsurveyanswerAction.class);
	public TsurveyanswerAction() {
	}
 
 
 	/**
	 * 问卷答案新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyanswerAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tsurveyanswer", new Tsurveyanswer());
        setDict(model);
		return "tsurveyanswerEdit";
	}

	/**
	 * 问卷答案新增保存
	 * 
	 * @param tsurveyanswer
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tsurveyanswerSave.do")
	public String save(Tsurveyanswer tsurveyanswer, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tsurveyanswer, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tsurveyanswer = tsurveyanswerService.save(tsurveyanswer);
		return "redirect:v_tsurveyanswerList.do";
	}

	/**
	 * 问卷答案删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tsurveyanswerDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tsurveyanswerService.remove(id);
			}
		}
		return "redirect:v_tsurveyanswerList.do";
	}

	/**
	 * 问卷答案编辑页面
	 * 
	 * @param answerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyanswerEdit.do")
	public String edit(long answerid, HttpServletRequest request, ModelMap model) {
		Tsurveyanswer tsurveyanswer = tsurveyanswerService.get(answerid);
		model.addAttribute("tsurveyanswer", tsurveyanswer);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tsurveyanswerEdit";
	}

	/**
	 * 问卷答案详情
	 * 
	 * @param answerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyanswerDetail.do")
	public String detail(long answerid, HttpServletRequest request, ModelMap model) {
		Tsurveyanswer tsurveyanswer = tsurveyanswerService.get(answerid);
//		model.addAttribute("tsurveyanswer", tsurveyanswer);
//        setDict(model);
		tsurveyanswerService.getCount(tsurveyanswer.getTsurveyquestion());
		return "tsurveyanswerView";
	}
	
	/**
	 * 问卷答案查询（分页）
	 * 
	 * @param tsurveyanswer
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyanswerList.do")
	public String listPage(Tsurveyanswer tsurveyanswer, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tsurveyanswerService.getTsurveyanswerByPage(toMap(tsurveyanswer),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tsurveyanswerList";
	}

	/**
	 * 问卷答案查询
	 * 
	 * @param tsurveyanswer
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyanswerList1.do")
	public String list(Tsurveyanswer tsurveyanswer, HttpServletRequest request, ModelMap model) {
		List<Tsurveyanswer> list = tsurveyanswerService.getTsurveyanswer(toMap(tsurveyanswer));
		model.addAttribute("list", list);
        setDict(model);
		return "tsurveyanswerList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tsurveyanswer
	 * @return
	 */	private Map<String,Object> toMap(Tsurveyanswer tsurveyanswer){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tsurveyanswer.getAnswerid()!=null&&!tsurveyanswer.getAnswerid().equals("")){
			map.put("answerid", tsurveyanswer.getAnswerid());
		}
		if(tsurveyanswer.getTsurveyquestion()!=null&&!tsurveyanswer.getTsurveyquestion().equals("")){
			map.put("tsurveyquestion", tsurveyanswer.getTsurveyquestion());
		}
		if(tsurveyanswer.getAnswer()!=null&&!tsurveyanswer.getAnswer().equals("")){
			map.put("answer", tsurveyanswer.getAnswer());
		}
		if(tsurveyanswer.getTsurveypartner()!=null&&!tsurveyanswer.getTsurveypartner().equals("")){
			map.put("tsurveypartner", tsurveyanswer.getTsurveypartner());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tsurveyanswer tsurveyanswer, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TsurveyanswerService tsurveyanswerService;   
}